package com.example.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

import android.accounts.AccountManager;
import android.content.Context;

public class PostUtil {

	public static final String APP_SERVER = "192.168.1.102";

	public static void post(String str, JSONObject jsonObject) {
		URL url;
		try {
			url = new URL(str);

			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "application/json");

			OutputStream os = conn.getOutputStream();
			String string = jsonObject.toString();
			os.write(string.getBytes());
			os.flush();

			if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			System.out.println("Output from Server .... \n");
			while ((output = br.readLine()) != null) {
				System.out.println(output);
			}

			conn.disconnect();

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void register(Context context, String registrationId) {
		System.out.println("Registered " + registrationId);
		JSONObject jsonObject = new JSONObject();
		try {
			String value = AccountManager.KEY_ACCOUNT_NAME;
			jsonObject.put("number", "test");
			jsonObject.put("name", value);
			jsonObject.put("gcmRegistration", registrationId);

			// Post regId to server
			PostUtil.post("http://" + APP_SERVER + ":8080/JAXB/rest/mobilePhones/mobilePhone", jsonObject );
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public static void unregister(Context context, String registrationId) {
		System.out.println("Unregistered " + registrationId);
		JSONObject jsonObject = new JSONObject();
		try {
			String value = AccountManager.KEY_ACCOUNT_NAME;
			jsonObject.put("number", "test");
			jsonObject.put("name", value);
			jsonObject.put("gcmRegistration", "");

			// Post regId to server
			PostUtil.put("http://" + APP_SERVER + ":8080/JAXB/rest/mobilePhones/mobilePhone", jsonObject );
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private static void put(String str, JSONObject jsonObject) {
		URL url;
		try {
			url = new URL(str);

			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("PUT");
			conn.setRequestProperty("Content-Type", "application/json");

			OutputStream os = conn.getOutputStream();
			String string = jsonObject.toString();
			os.write(string.getBytes());
			os.flush();

			if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			}

			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));

			String output;
			System.out.println("Output from Server .... \n");
			while ((output = br.readLine()) != null) {
				System.out.println(output);
			}

			conn.disconnect();

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
